﻿@inherits BaseComponent

<Tabs Class="ant-layout-tabs" Type="@TabType.EditableCard" HideAdd @bind-ActiveKey="activeKey" OnClose="OnClose" OnChange="OnChange">
    @foreach (var item in Items)
    {
        var itemName = Context.Language.GetString(item);
        <TabPane Key="@item.Id" Tab="@itemName" Closable="@item.Closable">
            @if (item.Page != null && item.Page.NoBackground)
            {
                <DynamicComponent @ref="dcs[item.Id]" Type="item.ComType" Parameters="item.ComParameters" />
            }
            else
            {
                <div class="kui-background">
                    <DynamicComponent @ref="dcs[item.Id]" Type="item.ComType" Parameters="item.ComParameters" />
                </div>
            }
        </TabPane>
    }
</Tabs>

@code {
    private string activeKey;
    private Dictionary<string, DynamicComponent> dcs = [];

    [CascadingParameter] private Admin Admin { get; set; }
    [Parameter] public List<Known.MenuItem> Items { get; set; }
    [Parameter] public Known.MenuItem Menu { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();
        activeKey = Menu.Id;
    }

    public override void StateChanged()
    {
        foreach (var dc in dcs)
        {
            (dc.Value.Instance as BaseComponent)?.StateChanged();
        }
    }

    private void OnClose(string tab)
    {
        var item = Items.FirstOrDefault(t => t.Id == tab);
        Items.Remove(item);
    }

    private void OnChange(string tab)
    {
        var item = Items.FirstOrDefault(t => t.Id == tab);
        Admin.SetCurrentMenu(item);
    }
}